//
//  groupViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/5.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "groupViewController.h"
#import "groupTableViewCell.h"
#import "coupleLiveAPI.h"
#import "AppDelegate.h"
#define APPDELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])
@interface groupViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *groupArr;
@end

@implementation groupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64)];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    [self.view addSubview:self.tableview];
    self.tableview.tableFooterView = [UIView new];
    self.groupArr=[[NSMutableArray alloc]init];
    [coupleLiveAPI nearbyGroupWithuid:APPDELEGATE.user.userid ugps:@"31.980942,120.9195" success:^(id response) {
        self.groupArr=response;
//        NSLog(@"%@",response);
        [self.tableview reloadData];
    }];
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.groupArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    groupTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"groupTableViewCell" owner:nil options:nil]firstObject];
    }
    //头像
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *decodedImageData   = [[NSData alloc] initWithBase64Encoding:[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"gr_img"]];
        UIImage *decodedImage      = [UIImage imageWithData:decodedImageData];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.headimage.image=decodedImage;
        });
    });
    //群名称
    cell.name.text=[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"gr_name"];
    //群成员
    cell.numlabel.text=[NSString stringWithFormat:@"%@",[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"member"]];
    cell.maxNum.text=[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"gr_perNums"];
    //群描述
    cell.disclabel.text=[NSString stringWithFormat:@"%@",[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"gr_introduce"]];
    //距离
//    NSLog(@"%@",[NSString stringWithFormat:@"%@",[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"km"]]);
    float m=[[NSString stringWithFormat:@"%@",[[self.groupArr objectAtIndex:indexPath.row]objectForKey:@"km"]] floatValue];
    cell.distancelabel.text=[NSString stringWithFormat:@"%.2fkm",m/1000];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
